package cn.unknowpupil.controller.system;

import cn.unknowpupil.common.core.annotation.Log;
import cn.unknowpupil.common.core.enums.BusinessType;
import cn.unknowpupil.common.core.resopnse.ResultResponse;
import cn.unknowpupil.common.core.utils.StringUtils;
import cn.unknowpupil.domain.dto.TreeSelect;
import cn.unknowpupil.domain.system.SysDept;
import cn.unknowpupil.service.system.ISysDeptService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created at 2020-10-21 by  w10g <br>
 * Initialize SysDept  ... <br>
 * 部门表 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Api(tags = {"系统-部门表模块"}, value = "SysDept")
@RestController
@RequestMapping(value = "admin/sys-dept")
public class SysDeptController {

    @Resource
    private ISysDeptService SysDeptService;

    /**
     * 新增
     *
     * @param SysDept {@link SysDept}  部门表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：新增")
    @Log(title = "create", businessType = BusinessType.INSERT)
    @PostMapping("create")
    public ResultResponse create(@Validated @RequestBody SysDept SysDept) {

        // 业务逻辑
        boolean created = SysDeptService.create(SysDept);
        if (created) {
            return ResultResponse.success("创建成功");
        } else {
            return ResultResponse.buildFailure("创建失败");
        }

    }

    /**
     * 修改
     *
     * @param SysDept {@link SysDept}  部门表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：修改")
    @Log(title = "update", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public ResultResponse update(@Validated @RequestBody SysDept SysDept) {

        // 业务逻辑
        boolean update = SysDeptService.update(SysDept);
        if (update) {
            return ResultResponse.success("修改成功");
        } else {
            return ResultResponse.buildFailure("修改失败");
        }

    }

    /**
     * 获取
     *
     * @param SysDeptId {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取")
    @GetMapping("get/{SysDeptId}")
    public ResultResponse get(@PathVariable("SysDeptId") Long SysDeptId) {
        SysDept SysDept = SysDeptService.info(SysDeptId);
        return ResultResponse.buildSuccess(SysDept);
    }


    /**
     * 获取
     *
     * @param deptId {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取")
    @GetMapping("list/exclude/{deptId}")
    public ResultResponse excludeList(@PathVariable("deptId") Long deptId) {
        List<SysDept> depts = SysDeptService.selectDeptList(new SysDept());
        Iterator<SysDept> it = depts.iterator();
        while (it.hasNext()) {
            SysDept d = (SysDept) it.next();
            if (d.getId().intValue() == deptId
                    || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + "")) {
                it.remove();
            }
        }
        return ResultResponse.buildSuccess(depts);
    }

    /**
     * 删除
     *
     * @param id {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：删除")
    @Log(title = "remove", businessType = BusinessType.DELETE)
    @DeleteMapping("remove/{id}")
    public ResultResponse remove(@PathVariable("id") Long id) {
        // 业务逻辑
        boolean deleted = SysDeptService.delete(id);
        if (deleted) {
            return ResultResponse.success("删除成功");
        } else {
            return ResultResponse.buildFailure("删除失败");
        }
    }

    /**
     * 不分页
     *
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：不分页")
    @GetMapping("list")
    public ResultResponse<List<SysDept>> selectList( @ModelAttribute SysDept sysDept) {
        List<SysDept> page = SysDeptService.selectList(sysDept);
        return ResultResponse.buildSuccess(page);
    }
    /**
     * 加载对应角色部门列表树
     */
    @GetMapping(value = "/roleDeptTree/{roleId}")
    public ResultResponse roleDeptTree(@PathVariable("roleId") Long roleId) {
        List<SysDept> depts = SysDeptService.selectDeptList(new SysDept());
        Map<String,Object> result = Maps.newHashMap();
        // 检查关键词
        result.put("checkedKeys",SysDeptService.selectDeptListByRoleId(roleId));
        result.put("depts", SysDeptService.buildDeptTreeSelect(depts));

        return ResultResponse.buildSuccess(result);

    }

    /**
     * 获取部门下拉树列表
     *
     * @param sysDept
     * @return
     */
    @ApiOperation("系统：获取部门下拉树列表")
    @GetMapping("tree")
    public ResultResponse<List<TreeSelect>> list(SysDept sysDept) {
        List<SysDept> list = SysDeptService.selectDeptList(sysDept);
        List<TreeSelect> treeSelects = SysDeptService.buildDeptTreeSelect(list);
        return ResultResponse.buildSuccess(treeSelects);
    }
}
